
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Xml;

using Swaf.Call;

using Swaf.Config;

namespace Swaf.BizRule
{
	public class RuleManagerServer
	{
		public RuleManagerServer(string name, string httpPort, string tcpPort)
		{
			if(!Application.globalApp.inBrowseMode)
			{
				IChannel channel = null;
				string url = "";
				string appUrl = "";


				if(tcpPort != null)
				{
					BinaryServerFormatterSinkProvider tpfProvider = new BinaryServerFormatterSinkProvider();
					tpfProvider.TypeFilterLevel = TypeFilterLevel.Full;

					BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

					IDictionary props = new Hashtable();
					props["port"] = Int32.Parse(tcpPort);
					props["name"] = name;

					channel = new TcpChannel( props, clientProvider, tpfProvider);
					ChannelServices.RegisterChannel(channel);
					RemotingConfiguration.RegisterWellKnownServiceType(typeof(RuleManagerService), name, WellKnownObjectMode.SingleCall);
				}

				if(httpPort != null)
				{
					SoapServerFormatterSinkProvider soapProvider = new SoapServerFormatterSinkProvider();
					soapProvider.TypeFilterLevel = TypeFilterLevel.Full;

					IDictionary props = new Hashtable();
					props["port"] = Int32.Parse(httpPort);
					props["name"] = "web" + name;

					channel = new HttpServerChannel( props, soapProvider);
				
					ChannelServices.RegisterChannel(channel);
					RemotingConfiguration.RegisterWellKnownServiceType(typeof(RuleManagerService), "web" + name, WellKnownObjectMode.Singleton);
				}
				Application thisApp = (Application) Application.currentApp;
				thisApp.OnAppShutdown += new AppShutdownRequest(shutDown);
			}
		}
		public void shutDown(IApplication app)
		{
			foreach (IChannel channel in ChannelServices.RegisteredChannels)
			{
				ChannelServices.UnregisterChannel(channel);
			}

		}

        private class BizRules : MarshalByRefObject
		{
			public string execute(string ruleName, string param, string paramType)
			{
				return Application.currentApp.ruleMgr.execute(ruleName, Serialization.deserialize(paramType,param)).getXml();
			}

			public string executeBatch(string ruleInfo)
			{
				return "";
			}
		}

	}

    public class RuleManagerService : MarshalByRefObject, ISimpleRuleExecutor 
	{
		IApplication m_app;
		IRuleManager m_ruleMgr;
		public RuleManagerService()
		{
			m_app = Application.currentApp;
			m_ruleMgr = m_app.ruleMgr;
		}

		#region ISimpleRuleExecutor Members

		public IResults execute(string ruleInfo)
		{
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(ruleInfo);
			return rc;//.getXml();
		}


		public IResults execute(string ruleName, ICollection callParams, bool multiCall)
		{
			// TODO:  Add RuleManagerService.Swaf.BizRule.IRuleExecutor.execute implementation
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(ruleName,callParams,multiCall);
			return rc;//.getXml();
		}

		public IResults execute(IRuleContext context, string[] ruleNames, object[] callParams)
		{
			// TODO:  Add RuleManagerService.Swaf.BizRule.IRuleExecutor.execute implementation
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(context, ruleNames,callParams);
			return rc;//.getXml();
		}

		public IResults execute(string[] ruleNames, object[] callParams)
		{
			// TODO:  Add RuleManagerService.Swaf.BizRule.IRuleExecutor.execute implementation
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(ruleNames,callParams);
			return rc;//.getXml();
		}

		public IResults execute(IRuleContext context, string ruleName, params object[] callParams)
		{
			// TODO:  Add RuleManagerService.Swaf.BizRule.IRuleExecutor.execute implementation
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(context, ruleName, callParams);
			return rc;//.getXml();
		}

		public IResults execute(string ruleName, params object[] callParams)
		{
			//System.Diagnostics.Debugger.Break();
			IResults rc = m_ruleMgr.execute(ruleName,callParams);
			return rc;//.getXml();
		}

		#endregion
	}
}
